﻿using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace QL_Truong_Hoc.DanhMuc
{
    public partial class frmHocSinhDangKy : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "tuyen_sinh";

        private clsEnum.FORM_MODE memMode;
        private int mintHSID = -1;

        public frmHocSinhDangKy(clsEnum.FORM_MODE emMode, int intHSID = clsConst.gcintNoneValue)
        {
            InitializeComponent();

            this.memMode = emMode;
            this.mintHSID = intHSID;

            if (emMode == clsEnum.FORM_MODE.ADD)
            {
                btnLuuAnh.Enabled = false;
                btnXoaAnh.Enabled = false;
            }
        }

        private void frmKhoa_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'qLTHDataSet.mDiaDanh' table. You can move, or remove it, as needed.
            this.mDiaDanhTableAdapter.Fill(this.qLTHDataSet.mDiaDanh);
            // TODO: This line of code loads data into the 'qLTHDataSet.mDanToc' table. You can move, or remove it, as needed.
            this.mDanTocTableAdapter.Fill(this.qLTHDataSet.mDanToc);
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }

            // TODO: This line of code loads data into the 'qLTHDataSet.mCapBac' table. You can move, or remove it, as needed.
            this.mCapBacTableAdapter.Fill(this.qLTHDataSet.mCapBac);
            // TODO: This line of code loads data into the 'qLTHDataSet.mTrinhDoVanHoa' table. You can move, or remove it, as needed.
            this.mTrinhDoVanHoaTableAdapter.Fill(this.qLTHDataSet.mTrinhDoVanHoa);
            // TODO: This line of code loads data into the 'qLTHDataSet.mTrinhDoDaoTao' table. You can move, or remove it, as needed.
            this.mTrinhDoDaoTaoTableAdapter.Fill(this.qLTHDataSet.mTrinhDoDaoTao);
            // TODO: This line of code loads data into the 'qLTHDataSet.mNghe' table. You can move, or remove it, as needed.
            this.mNgheTableAdapter.Fill(this.qLTHDataSet.mNghe);
            // TODO: This line of code loads data into the 'qLTHDataSet.mDoiTuongUuTien' table. You can move, or remove it, as needed.
            this.mDoiTuongUuTienTableAdapter.Fill(this.qLTHDataSet.mDoiTuongUuTien);
            // TODO: This line of code loads data into the 'qLTHDataSet.mDonViQuanDoi' table. You can move, or remove it, as needed.
            this.mDonViQuanDoiTableAdapter.Fill(this.qLTHDataSet.mDonViQuanDoi);

            //fill năm tuyển sinh
            for (int i = 0; i <= 5; i++)
            {
                cbNamTuyenSinh.Properties.Items.Add(DateTime.Now.Year - i);
            }

            //fill đợt tuyển sinh
            cbDotTuyenSinh.Properties.Items.Add("Đợt 1");
            cbDotTuyenSinh.Properties.Items.Add("Đợt 2");
            cbDotTuyenSinh.Properties.Items.Add("Đợt 3");
            cbDotTuyenSinh.Properties.Items.Add("Đợt 4");

            //fill năm tốt nghiệp
            txtNamTotNghiep.Value = DateTime.Now.Year;

            rdGioiTinh.EditValue = (int)clsEnum.GIOI_TINH.NAM;


            if (memMode == clsEnum.FORM_MODE.EDIT) { xLoadData(mintHSID); }
            else { xClear(); }
        }

        private bool xClear()
        {
            bool blnResult = false;
            try
            {
                //đối tượng học sinh 
                txtHoDem.Text = "";
                txtTen.Text = "";
                rdGioiTinh.EditValue = null;
                dtpNgaySinh.EditValue = null;
                txtQueQuan.Text = "";
                txtDiaDanh.EditValue = null;
                chkDaKhaoSatTruoc.Checked = false;
                txtThuongTru.Text = "";

                lokDonVi.EditValue = null;
                lokCapBac.EditValue = null;
                dtpNgayVaoDang.EditValue = null;
                dtpNgayVaoDoan.EditValue = null;
                cbDanToc.EditValue = null;

                lokTrinhDoVanHoa.EditValue = null;
                txtNamTotNghiep.Value = DateTime.Now.Year;
                txtNoiTotNghiep.Text = "";
                lokDoiTuongUuTien.EditValue = null;
                txtCMND.Text = "";

                dtpCMND_NgayCap.EditValue = null;
                txtCMND_NoiCap.Text = "";
                txtHoTenCha.Text = "";
                txtHoTenMe.Text = "";
                txtDiaChiLienLac.Text = "";

                txtDienThoaiNR.Text = "";
                txtDienThoaiDD.Text = "";

                //thông tin dự tuyển

                cbNamTuyenSinh.EditValue = null;
                cbDotTuyenSinh.EditValue = null;
                lokTrinhDoDaoTao.EditValue = null;
                lokNguyenVong1.EditValue = null;
                lokNguyenVong2.EditValue = null;

                chkPhieu.UnCheckAll();

                txtQDXN_So.Text = "";
                dtpQDXN_Ngay.EditValue = null;
                txtTheHocNghe_So.Text = "";
                dtpTheHocNghe_Ngay.EditValue = null;

                grpQDXuatNgu.Enabled = false;
                grpTheHocNghe.Enabled = false;

                txtHoDem.Focus();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return blnResult;
        }

        private bool xLoadData(int intHsID)
        {
            bool blnResult = false;
            try
            {
                DTO.THocSinhDTO objHocSinh = DAO.THocSinhDAO.Select(Program.gobjDb, intHsID);
                DTO.TDanhSachDuTuyenDTO objDuTuyen = DAO.TDanhSachDuTuyenDAO.Select(Program.gobjDb, intHsID);
                DTO.THocSinhAnhDTO objAnh = DAO.THocSinhAnhDAO.Select(Program.gobjDb, intHsID);

                //đối tượng học sinh 
                txtHoDem.Text = objHocSinh.Hs_hodem;
                txtTen.Text = objHocSinh.Hs_ten;
                rdGioiTinh.EditValue = clsCommon.fncCnvNullToBool(objHocSinh.Hs_gioitinh);
                if (objHocSinh.Hs_ngaysinh > DateTime.MinValue) dtpNgaySinh.EditValue = objHocSinh.Hs_ngaysinh;
                txtQueQuan.Text = objHocSinh.Hs_quequan;
                txtDiaDanh.EditValue = objHocSinh.DD_ID;
                chkDaKhaoSatTruoc.Checked = objHocSinh.Hs_khaosattruockhihoc;
                txtThuongTru.Text = objHocSinh.Hs_hokhau;

                lokDonVi.EditValue = objHocSinh.DV_ID;
                lokCapBac.EditValue = objHocSinh.CB_ID;
                if (objHocSinh.Hs_ngayvaodang > DateTime.MinValue) dtpNgayVaoDang.EditValue = objHocSinh.Hs_ngayvaodang;
                if (objHocSinh.Hs_ngayvaodoan > DateTime.MinValue) dtpNgayVaoDoan.EditValue = objHocSinh.Hs_ngayvaodoan;
                cbDanToc.EditValue = objHocSinh.Hs_dantoc;

                lokTrinhDoVanHoa.EditValue = objHocSinh.TDVH_ID;
                txtNamTotNghiep.Value = DateTime.Now.Year;
                txtNoiTotNghiep.Text = objHocSinh.Hs_noitotnghiep;
                lokDoiTuongUuTien.EditValue = objHocSinh.Hs_uutien;
                txtCMND.Text = objHocSinh.Hs_cmnd;

                if (objHocSinh.Hs_cmnd_ngaycap > DateTime.MinValue) dtpCMND_NgayCap.EditValue = objHocSinh.Hs_cmnd_ngaycap;
                txtCMND_NoiCap.Text = objHocSinh.Hs_cmnd_noicap;
                txtHoTenCha.Text = objHocSinh.Hs_hotencha;
                txtHoTenMe.Text = objHocSinh.Hs_hotenme;
                txtDiaChiLienLac.Text = objHocSinh.Hs_diachilienlac;

                txtDienThoaiNR.Text = objHocSinh.Hs_dienthoainr;
                txtDienThoaiDD.Text = objHocSinh.Hs_dienthoaidd;

                //thông tin dự tuyển

                cbNamTuyenSinh.EditValue = objDuTuyen.NamTuyenSinh;
                cbDotTuyenSinh.EditValue = objDuTuyen.DotTuyenSinh;
                lokTrinhDoDaoTao.EditValue = objDuTuyen.TD_ID;
                lokNguyenVong1.EditValue = objDuTuyen.NG_ID1;
                lokNguyenVong2.EditValue = objDuTuyen.NG_ID2;

                int i = 0; if (objDuTuyen.PhieuHocNghe) chkPhieu.Items[i].CheckState = System.Windows.Forms.CheckState.Checked;
                i++; if (objDuTuyen.TheHocNghe) chkPhieu.Items[i].CheckState = System.Windows.Forms.CheckState.Checked;
                i++; if (objDuTuyen.QuyetDinhXuatNgu) chkPhieu.Items[i].CheckState = System.Windows.Forms.CheckState.Checked;
                i++; if (objDuTuyen.PhieuQuanNhan) chkPhieu.Items[i].CheckState = System.Windows.Forms.CheckState.Checked;
                i++; if (objDuTuyen.CMND) chkPhieu.Items[i].CheckState = System.Windows.Forms.CheckState.Checked;
                i++; if (objDuTuyen.BangVanHoa) chkPhieu.Items[i].CheckState = System.Windows.Forms.CheckState.Checked;
                i++; if (objDuTuyen.BanSaoGiayKhaiSinh) chkPhieu.Items[i].CheckState = System.Windows.Forms.CheckState.Checked;
                i++; if (objDuTuyen.Anh) chkPhieu.Items[i].CheckState = System.Windows.Forms.CheckState.Checked;
                i++; if (objDuTuyen.DaCapVPP) chkPhieu.Items[i].CheckState = System.Windows.Forms.CheckState.Checked;

                txtQDXN_So.Text = objDuTuyen.QuyetDinhXuatNgu_So;
                if (objDuTuyen.QuyetDinhXuatNgu_Ngay > DateTime.MinValue) dtpQDXN_Ngay.EditValue = objDuTuyen.QuyetDinhXuatNgu_Ngay;
                txtTheHocNghe_So.Text = objDuTuyen.TheHocNghe_So;
                if (objDuTuyen.TheHocNghe_Ngay > DateTime.MinValue) dtpTheHocNghe_Ngay.EditValue = objDuTuyen.TheHocNghe_Ngay;

                //grpQDXuatNgu.Enabled = false;
                //if (objDuTuyen.QuyetDinhXuatNgu) grpQDXuatNgu.Enabled = true;

                //grpTheHocNghe.Enabled = false;
                //if (objDuTuyen.TheHocNghe) grpTheHocNghe.Enabled = true;

                //ảnh đại diện
                if (objAnh.Hs_anhdaidien != null)
                {
                    ImageConverter ic = new ImageConverter();
                    pbAvatar.Image = (Image)ic.ConvertFrom(objAnh.Hs_anhdaidien);
                }

                this.Text = string.Format("Đăng ký - {0} {1}", objHocSinh.Hs_hodem, objHocSinh.Hs_ten);

                txtHoDem.Focus();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return blnResult;
        }

        private bool xGetData(int intHsID, DTO.THocSinhDTO objHocSinh, DTO.TDanhSachDuTuyenDTO objDuTuyen)
        {
            bool blnResult = false;
            try
            {
                int intTemp;

                //đối tượng học sinh
                objHocSinh.HS_ID = intHsID;
                objHocSinh.Hs_hodem = txtHoDem.Text.Trim();
                objHocSinh.Hs_ten = txtTen.Text.Trim();
                objHocSinh.Hs_gioitinh = clsCommon.fncCnvBoolToInt(rdGioiTinh.EditValue);
                objHocSinh.Hs_ngaysinh = clsCommon.fncCnvNullToDatetime(dtpNgaySinh.EditValue);
                objHocSinh.Hs_quequan = txtQueQuan.Text.Trim();
                objHocSinh.DD_ID = clsCommon.fncCnvNullToString(txtDiaDanh.EditValue);
                objHocSinh.Hs_khaosattruockhihoc = chkDaKhaoSatTruoc.Checked;
                objHocSinh.Hs_hokhau = txtThuongTru.Text;

                objHocSinh.DV_ID = clsCommon.fncCnvNullToInt(lokDonVi.EditValue);
                objHocSinh.CB_ID = clsCommon.fncCnvNullToInt(lokCapBac.EditValue);
                objHocSinh.Hs_ngayvaodang = clsCommon.fncCnvNullToDatetime(dtpNgayVaoDang.EditValue);
                objHocSinh.Hs_ngayvaodoan = clsCommon.fncCnvNullToDatetime(dtpNgayVaoDoan.EditValue);
                objHocSinh.Hs_dantoc = clsCommon.fncCnvNullToString(cbDanToc.EditValue);

                objHocSinh.TDVH_ID = clsCommon.fncCnvNullToInt(lokTrinhDoVanHoa.EditValue);
                intTemp = (int)txtNamTotNghiep.Value; if (intTemp > 0) objHocSinh.Hs_namtotnghiep = intTemp;
                objHocSinh.Hs_noitotnghiep = txtNoiTotNghiep.Text;
                objHocSinh.Hs_uutien = clsCommon.fncCnvNullToString(lokDoiTuongUuTien.EditValue);
                objHocSinh.Hs_cmnd = txtCMND.Text;

                objHocSinh.Hs_cmnd_ngaycap = clsCommon.fncCnvNullToDatetime(dtpNgaySinh.EditValue);
                objHocSinh.Hs_cmnd_noicap = txtCMND_NoiCap.Text;
                objHocSinh.Hs_hotencha = txtHoTenCha.Text;
                objHocSinh.Hs_hotenme = txtHoTenMe.Text;
                objHocSinh.Hs_diachilienlac = txtDiaChiLienLac.Text;

                objHocSinh.Hs_dienthoainr = txtDienThoaiNR.Text;
                objHocSinh.Hs_dienthoaidd = txtDienThoaiDD.Text;

                //thông tin dự tuyển
                objDuTuyen.HS_ID = intHsID;
                objDuTuyen.NamTuyenSinh = clsCommon.fncCnvNullToInt(cbNamTuyenSinh.EditValue);
                objDuTuyen.DotTuyenSinh = clsCommon.fncCnvNullToString(cbDotTuyenSinh.EditValue);
                objDuTuyen.TD_ID = clsCommon.fncCnvNullToInt(lokTrinhDoDaoTao.EditValue);
                objDuTuyen.NG_ID1 = clsCommon.fncCnvNullToInt(lokNguyenVong1.EditValue);
                objDuTuyen.NG_ID2 = clsCommon.fncCnvNullToInt(lokNguyenVong2.EditValue);

                int i = 0; if (chkPhieu.Items[i].CheckState == System.Windows.Forms.CheckState.Checked) { objDuTuyen.PhieuHocNghe = true; } else { objDuTuyen.PhieuHocNghe = false; }
                i++; if (chkPhieu.Items[i].CheckState == System.Windows.Forms.CheckState.Checked) { objDuTuyen.TheHocNghe = true; } else { objDuTuyen.TheHocNghe = false; }
                i++; if (chkPhieu.Items[i].CheckState == System.Windows.Forms.CheckState.Checked) { objDuTuyen.QuyetDinhXuatNgu = true; } else { objDuTuyen.QuyetDinhXuatNgu = false; }
                i++; if (chkPhieu.Items[i].CheckState == System.Windows.Forms.CheckState.Checked) { objDuTuyen.PhieuQuanNhan = true; } else { objDuTuyen.PhieuQuanNhan = false; }
                i++; if (chkPhieu.Items[i].CheckState == System.Windows.Forms.CheckState.Checked) { objDuTuyen.CMND = true; } else { objDuTuyen.CMND = false; }
                i++; if (chkPhieu.Items[i].CheckState == System.Windows.Forms.CheckState.Checked) { objDuTuyen.BangVanHoa = true; } else { objDuTuyen.BangVanHoa = false; }
                i++; if (chkPhieu.Items[i].CheckState == System.Windows.Forms.CheckState.Checked) { objDuTuyen.BanSaoGiayKhaiSinh = true; } else { objDuTuyen.BanSaoGiayKhaiSinh = false; }
                i++; if (chkPhieu.Items[i].CheckState == System.Windows.Forms.CheckState.Checked) { objDuTuyen.Anh = true; } else { objDuTuyen.Anh = false; }
                i++; if (chkPhieu.Items[i].CheckState == System.Windows.Forms.CheckState.Checked) { objDuTuyen.DaCapVPP = true; } else { objDuTuyen.DaCapVPP = false; }

                objDuTuyen.QuyetDinhXuatNgu_So = "";
                objDuTuyen.QuyetDinhXuatNgu_Ngay = DateTime.MinValue;
                objDuTuyen.TheHocNghe_So = "";
                objDuTuyen.TheHocNghe_Ngay = DateTime.MinValue;

                if (objDuTuyen.QuyetDinhXuatNgu)
                {
                    objDuTuyen.QuyetDinhXuatNgu_So = txtQDXN_So.Text.Trim();
                    objDuTuyen.QuyetDinhXuatNgu_Ngay = clsCommon.fncCnvNullToDatetime(dtpQDXN_Ngay.EditValue);
                }

                if (objDuTuyen.TheHocNghe)
                {
                    objDuTuyen.TheHocNghe_So = txtTheHocNghe_So.Text.Trim();
                    objDuTuyen.TheHocNghe_Ngay = clsCommon.fncCnvNullToDatetime(dtpTheHocNghe_Ngay.EditValue);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return blnResult;
        }

        private void mniSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                //check valid trước khi tiếp tục
                if (!validControlBlank.Validate()) return;

                if (clsCommon.fncMsgConfirm("Dữ liệu sẽ được lưu. Bạn có chắc chắn?"))
                {
                    if (memMode == clsEnum.FORM_MODE.ADD)
                    {
                        if (xAddNew())
                        {
                            if (clsCommon.fncMsgConfirm("Lưu dữ liệu thành công. Bạn có muốn lưu dữ liệu khác?"))
                            {
                                this.memMode = clsEnum.FORM_MODE.ADD;
                                xClear();
                            }
                            else
                            { this.Close(); }
                        }
                        else
                        {
                            clsCommon.fncMsgError("Lưu dữ liệu không thành công. Hãy kiểm tra lại dữ liệu và thử lại.");
                        }
                    }
                    else
                    {
                        if (xUpdate())
                        {
                            clsCommon.fncMsgInfo("Lưu dữ liệu thành công.");
                            this.Close();
                        }
                        else
                        {
                            clsCommon.fncMsgError("Lưu dữ liệu không thành công. Hãy kiểm tra lại dữ liệu và thử lại.");
                        }
                    }
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private bool xAddNew()
        {
            bool blnResult = false;
            try
            {
                int intHsID = DAO.THocSinhDAO.SelectNextIndex(Program.gobjDb);

                //khai báo đối tượng
                DTO.THocSinhDTO objHocSinh = new DTO.THocSinhDTO();
                DTO.TDanhSachDuTuyenDTO objDuTuyen = new DTO.TDanhSachDuTuyenDTO();

                xGetData(intHsID, objHocSinh, objDuTuyen);

                bool blnSuccess = true;

                //Program.mobjConn.Open();
                //DbTransaction objTrans = objConn.BeginTransaction();
                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    blnSuccess = blnSuccess && DAO.THocSinhDAO.Insert(Program.gobjDb, Program.gobjTrans, objHocSinh);
                    blnSuccess = blnSuccess && DAO.TDanhSachDuTuyenDAO.Insert(Program.gobjDb, Program.gobjTrans, objDuTuyen);

                    //commit dữ liệu nếu không có gì lỗi, ngược lại thì rollback
                    if (blnSuccess)
                    {
                        Program.gobjTrans.Commit();
                        return true;
                    }
                    else
                    { Program.gobjTrans.Rollback(); }
                }
                catch
                {
                    Program.gobjTrans.Rollback();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return blnResult;
        }

        private bool xUpdate()
        {
            bool blnResult = false;
            try
            {
                int intHsID = mintHSID;

                //khai báo đối tượng
                DTO.THocSinhDTO objHocSinh = DAO.THocSinhDAO.Select(Program.gobjDb, intHsID);
                DTO.TDanhSachDuTuyenDTO objDuTuyen = DAO.TDanhSachDuTuyenDAO.Select(Program.gobjDb, intHsID);

                xGetData(intHsID, objHocSinh, objDuTuyen);

                bool blnSuccess = true;

                //objConn.Open();
                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    blnSuccess = blnSuccess && DAO.THocSinhDAO.Update(Program.gobjDb, Program.gobjTrans, objHocSinh);
                    blnSuccess = blnSuccess && DAO.TDanhSachDuTuyenDAO.Update(Program.gobjDb, Program.gobjTrans, objDuTuyen);

                    //commit dữ liệu nếu không có gì lỗi, ngược lại thì rollback
                    if (blnSuccess)
                    {
                        Program.gobjTrans.Commit();
                        return true;
                    }
                    else
                    { Program.gobjTrans.Rollback(); }
                }
                catch
                {
                    Program.gobjTrans.Rollback();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return blnResult;
        }

        private void mniCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (clsCommon.fncMsgConfirm("Dữ liệu sẽ không được lưu. Bạn có chắc chắn?"))
                {
                    this.Close();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void chkPhieu_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            try
            {
                if (chkPhieu.Items[1].CheckState == System.Windows.Forms.CheckState.Checked)
                    grpTheHocNghe.Enabled = true;
                else
                    grpTheHocNghe.Enabled = false;

                if (chkPhieu.Items[2].CheckState == System.Windows.Forms.CheckState.Checked)
                    grpQDXuatNgu.Enabled = true;
                else
                    grpQDXuatNgu.Enabled = false;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void txtHoDem_Leave(object sender, EventArgs e)
        {
            try
            {
                DevExpress.XtraEditors.TextEdit objText = (DevExpress.XtraEditors.TextEdit)sender;
                objText.Text = clsCommon.fncCapitalizeFirst(objText.Text.Trim());
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void pbAvatar_Click(object sender, EventArgs e)
        {
            try
            {
                if (dlgOpenImage.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

                using (Tools.CatAnh.frmCatAnh objAnh = new Tools.CatAnh.frmCatAnh(dlgOpenImage.FileName))
                {
                    objAnh.ShowDialog();
                    if (!objAnh.ImageSelected) return;

                    pbAvatar.Image = objAnh.ImageHS;
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnXoaAnh_Click(object sender, EventArgs e)
        {
            try
            {
                pbAvatar.Image = null;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnLuuAnh_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                DTO.THocSinhAnhDTO objAnh = new DTO.THocSinhAnhDTO();
                objAnh.HS_ID = mintHSID;
                objAnh.Hs_anhdaidien = clsCommon.fncCnvImageToByte(pbAvatar.Image);

                DAO.THocSinhAnhDAO.Delete(Program.gobjDb, Program.gobjTrans, mintHSID);
                DAO.THocSinhAnhDAO.Insert(Program.gobjDb, Program.gobjTrans, objAnh);

                clsCommon.fncMsgInfo("Đã lưu ảnh đại diện.");
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

    }
}